package com.example.demo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    public static final String EXCHANGE_NAME = "alarm.notification.delay.exchange";
    public static final String QUEUE_NAME = "alarm.notification.delay.queue";  // 修改队列名称
    public static final String ROUTING_KEY = "alarm.notification.delay";

    @Bean
    public Queue alarmNotificationQueue() {
        // 添加队列参数以支持持久化
        Map<String, Object> args = new HashMap<>();
        args.put("x-queue-type", "classic");
        return new Queue(QUEUE_NAME, true, false, false, args);
    }

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding bindingNotificationQueue() {
        return BindingBuilder.bind(alarmNotificationQueue())
                .to(delayExchange())
                .with(ROUTING_KEY)
                .noargs();
    }
}